package com.zp.common.pdf;

import com.zp.common.core.exception.ServiceException;
import org.apache.pdfbox.pdmodel.PDDocument;
import org.apache.pdfbox.printing.PDFPrintable;
import org.apache.pdfbox.printing.Scaling;

import javax.print.PrintService;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.standard.Sides;
import java.awt.print.*;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 打印机打印
 * </p>
 *
 * @author zp
 * @since 2019/8/29
 */
public class HisPrint {
    /**
     * 文件打印
     *
     * @param filePath
     * @param printerName
     */
    public static void printReport (String filePath, String printerName) {
        PDDocument document = null;
        try {
            File file = new File(filePath);
            try {

                document = PDDocument.load(file);
            } catch (IOException e) {
                throw new ServiceException("加载文件失败：" + file.getName());
            }
            PrinterJob printJob = PrinterJob.getPrinterJob();
            printJob.setJobName(file.getName());
            if (printerName != null) {
                // 查找并设置打印机
                //获得本台电脑连接的所有打印机
                PrintService[] printServices = PrinterJob.lookupPrintServices();
                if (printServices == null || printServices.length == 0) {
                    throw new ServiceException("未找到打印机");
                }
                PrintService printService = null;
                //匹配指定打印机
                for (int i = 0; i < printServices.length; i++) {
                    if (printServices[i].getName().contains(printerName)) {
                        printService = printServices[i];
                        break;
                    }
                }
                if (printService != null) {
                    try {
                        printJob.setPrintService(printService);
                    } catch (PrinterException e) {
                        throw new ServiceException("设置打印工作错误：" + printerName);
                    }
                } else {
                    throw new ServiceException("未找到名称为" + printerName + "的打印机");
                }
            }
            //设置纸张及缩放
            PDFPrintable pdfPrintable = new PDFPrintable(document, Scaling.ACTUAL_SIZE);
            //设置多页打印
            Book book = new Book();
            PageFormat pageFormat = new PageFormat();
            //设置打印方向
            //纵向
            pageFormat.setOrientation(PageFormat.PORTRAIT);
            //设置纸张
            pageFormat.setPaper(getPaper());
            book.append(pdfPrintable, pageFormat, document.getNumberOfPages());
            printJob.setPageable(book);
            //设置打印份数
            printJob.setCopies(1);
            //添加打印属性
            HashPrintRequestAttributeSet pars = new HashPrintRequestAttributeSet();
            //设置单双页
            pars.add(Sides.DUPLEX);
            try {
                printJob.print(pars);
            } catch (PrinterException e) {
                throw new ServiceException("打印文件失败：" + file.getName());
            }
        } finally {
            if (document != null) {
                try {
                    document.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 获取页面相关配置
     *
     * @return
     */
    public static Paper getPaper () {
        Paper paper = new Paper();
        // 默认为A4纸张，对应像素宽和高分别为 595, 842
        int width = 595;
        int height = 842;
        // 设置边距，单位是像素，10mm边距，对应 28px
        int marginLeft = 10;
        int marginRight = 0;
        int marginTop = 10;
        int marginBottom = 0;
        paper.setSize(width, height);
        // 下面一行代码，解决了打印内容为空的问题
        paper.setImageableArea(marginLeft, marginRight, width - (marginLeft + marginRight), height - (marginTop + marginBottom));
        return paper;
    }

    /**
     * 查询打印机列表
     *
     * @return
     */
    public static List<String> listPrint () {
        List<String> printList = new ArrayList<>();
        PrintService[] printServices = PrinterJob.lookupPrintServices();
        for (int i = 0; i < printServices.length; i++) {
            printList.add(printServices[i].getName());
        }
        return printList;
    }

    public static void main (String[] args) {
        printReport("fapiao", "NPI");
    }
}
